<?php
class Data extends Model_Abstract
{
	public function addAdvertiserData($advertiser, $site, $placement, $click, $revenue, $date) {
		$sql = "insert into advertiser_data set advertiser_id=?, site_id=?, placement=?, click=?, revenue=?, data_time=?";
		try {
			$result = $this->_connection->execute($sql, array($advertiser, $site, $placement, $click, $revenue, date('Ymd', strtotime($date))));
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	/*
	public function getAllAdvertiserData()
	{
		$sql = "SELECT ad.id, ad.placement, ad.click, ad.revenue, ad.data_time, s.name as site_name, a.name as advertiser_name
			FROM advertiser_data AS ad, advertisers AS a, sites AS s
			WHERE s.id = ad.site_id
			AND a.id = ad.advertiser_id";
		try {
			$result = $this->_connection->query($sql);
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	*/
	
	public function editAdvertiserData($id, $advertiser, $site, $placement, $click, $revenue, $date) {
		$sql = "update advertiser_data set advertiser_id=?, site_id=?, placement=?, click=?, revenue=?, data_time=? where id=?";
		try {
			$result = $this->_connection->execute($sql, array($advertiser, $site, $placement, $click, $revenue, date('Ymd', strtotime($date)), $id));
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function getAdvertiserDataById($id) {
		$sql = "select * from advertiser_data where id=?";
		try {
			$result = $this->_connection->query($sql, array($id));
			if (count($result) > 0) 
				return $result[0];
			return false;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function deleteAdvertiserDataById($id) {
		$sql = "delete from advertiser_data where id=?";
		try {
			$result = $this->_connection->execute($sql, array($id));
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function search($advertiserId, $siteId, $dateStart, $dateEnd) {
		$condtion = array();
		$vars = array();
		if (!empty($advertiserId)) {
			$condition[] = 'advertiser_id=?';
			$vars[] = $advertiserId;
		}
		if (!empty($siteId)) {
			$condition[] = 'site_id=?';
			$vars[] = $siteId;
		}
		if (!empty($dateStart)) {
			$condition[] = "data_time>=?";
			$vars[] = $dateStart;
		}
		if (!empty($dateEnd)) {
			$condition[] = 'data_time<=?';
			$vars[] = $dateEnd;
		}
		$sql = "select * from advertiser_data ";
		if (count($condition) > 0) {
			$sql .= 'where ' . implode(' and ', $condition);
		}
		try {
			$result = $this->_connection->query($sql, $vars);
			if (count($result) > 0) 
				return $result;
			return false;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
}